package com.agriculture.controller;

import com.agriculture.common.R;
import com.agriculture.entity.ViewOrder;
import com.agriculture.service.ViewOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/viewOrder")
public class ViewOrderController {

    @Autowired
    private ViewOrderService viewOrderService;


    /**
     * 根据 order_id 查询 view_order
     * @param orderId
     * @return
     */
    @GetMapping("/list")
    public R getList(Long orderId){
        LambdaQueryWrapper<ViewOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ViewOrder::getOrderId, orderId);
        return R.success(viewOrderService.list(queryWrapper));
    }

    /**
     * 根据 order_id 分页查询 view_order
     * @param page
     * @param pageSize
     * @param orderId
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long orderId){

        LambdaQueryWrapper<ViewOrder> queryWrapper = new LambdaQueryWrapper<>();

        //分页构造器
        Page<ViewOrder> pageInfo = new Page<>(page,pageSize);

        //添加排序条件，根据Level进行排序
        queryWrapper.eq(ViewOrder::getOrderId, orderId);

        //分页查询
        Page<ViewOrder> ViewOrderPage = viewOrderService.page(pageInfo, queryWrapper);
        return R.success(ViewOrderPage);
    }

    /**
     * 根据 order_id 分页查询 view_order
     * @param page
     * @param pageSize
     * @param orderId
     * @return
     */
    @GetMapping("/allpage")
    public R<Page> page(int page, int pageSize){

//        LambdaQueryWrapper<ViewOrder> queryWrapper = new LambdaQueryWrapper<>();

        //分页构造器
        Page<ViewOrder> pageInfo = new Page<>(page,pageSize);

        //添加排序条件，根据Level进行排序
//        queryWrapper.eq(ViewOrder::getOrderId, orderId);

        //分页查询
        Page<ViewOrder> ViewOrderPage = viewOrderService.page(pageInfo);
        return R.success(ViewOrderPage);
    }


    @GetMapping("/getByUserId")
    public R getByUserId(@RequestParam Long userId){
        LambdaQueryWrapper<ViewOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ViewOrder::getUserId, userId);
        List<ViewOrder> viewOrderList = viewOrderService.list(queryWrapper);
        return R.success(viewOrderList);
    }

}





















